﻿using HexParse.Common;
using HexParse.Config;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using HexParse.Communications;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Documents;
using HexParse.Datas;
using HexParse.Controls.Windows;

namespace HexParse.Controls.PageControls
{
    /// <summary>
    /// 常规页面
    /// </summary>
    public partial class BootloaderPage : BeatfanControls.UserControls.MyUserControlModel
    {


        #region 数据定义
        /// <summary>
        /// 是否已经打开CAN
        /// </summary>
        bool m_BOpen = false;

        /// <summary>
        /// 串口操作
        /// </summary>
        ClassUartOperation m_UartOperate = new ClassUartOperation();

        /// <summary>
        /// 定时发送定时器
        /// </summary>
        private System.Windows.Threading.DispatcherTimer m_AutoSendTimer = new System.Windows.Threading.DispatcherTimer();

        /// <summary>
        /// Hex文件转换
        /// </summary>
        MicrochipHexCommonFunctions m_MicrochipCommonFunction = new MicrochipHexCommonFunctions();

        /// <summary>
        /// 烧录中
        /// </summary>
        bool m_IsBurnning = false;

        /// <summary>
        /// 发送数据是否收到回馈
        /// </summary>
        bool m_SendHasReponse = false;

        /// <summary>
        /// 串口数据解析类
        /// </summary>
        ClassBootloaderForUart m_UartDataParse = new ClassBootloaderForUart();

        /// <summary>
        /// 所有行数据
        /// </summary>
        //List<byte[]> m_AllLineDatas;


        /// <summary>
        /// 所有行数据的拆解后的byte数组集合
        /// </summary>
        //List<ClassBootloaderForUart.OneFrame> m_AllLineSplitSendData = new List<ClassBootloaderForUart.OneFrame>();

        /// <summary>
        /// 每个地址对应的数据
        /// </summary>
        DataSource.AddrDataListClass m_AllAddrData = new DataSource.AddrDataListClass();

        /// <summary>
        /// 已发送数据统计
        /// </summary>
        int m_SendDataCount = 0;

        /// <summary>
        /// 已接收数据统计
        /// </summary>
        int m_ReceiveDataCount = 0;

        /// <summary>
        /// 是否处于Bootloader中
        /// </summary>
        bool m_InBootloader = false;

        /// <summary>
        /// 开始时间，方便统计使用时间
        /// </summary>
        DateTime m_StartSendDateTime;

        /// <summary>
        /// 芯片用户存储部分是否已经擦除完毕
        /// </summary>
        bool m_McuFlashIsErased = false;
        #endregion

        #region 构造函数
        public BootloaderPage()
        {
            InitializeComponent();


            m_AutoSendTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            //m_AutoSendTimer.Tick += M_AutoSendTimer_Tick;

            //初始化下拉框
            InitialAll();
        }




        #endregion

        #region 初始化
        /// <summary>
        /// 初始化设备索引，第几路CAN，波特率选择框
        /// </summary>
        private void InitialAll()
        {
            //加载上次配置
            LoadConfig();

            //初始化绑定源
            InitialBindingData();

            //UI
            Connect_UIChange(false);
        }

        /// <summary>
        /// 刷新串口
        /// </summary>
        void GetUart()
        {
            //获取系统的串口设备
            cbUartPort.ItemsSource = ClassCommonUart.MulGetHardwareInfo(ClassCommonUart.HardwareEnum.Win32_PnPEntity, "Name");
        }

        /// <summary>
        /// 绑定数据处理
        /// </summary>
        private void InitialBindingData()
        {
            #region 下拉框初始化
            GetUart();
            cbUartPort.SelectedIndex = 0;
            m_UartOperate.SerialInitial(115200, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, ReceiveData);

            cbUartBaudrate.ItemsSource = new string[] { "115200" };
            cbUartBaudrate.SelectedIndex = 0;
            cbUartDataLength.ItemsSource = new string[] { "8" };
            cbUartDataLength.SelectedIndex = 0;
            cbUartParity.ItemsSource = new string[] { "NONE" };
            cbUartParity.SelectedIndex = 0;
            cbUartStopbit.ItemsSource = new string[] { "1" };
            cbUartStopbit.SelectedIndex = 0;
            #endregion

        }
        #endregion

        #region UI变换
        /// <summary>
        /// 开发模式的UI
        /// 0为CAN，1为串口
        /// </summary>
        private void Connect_UIChange(bool bState)
        {

            gdUartInfo.IsEnabled = !bState;
            gdMsg.IsEnabled = gdBody.IsEnabled = bState;

            if (bState)
                btnUartConnect.Content = "DisConnect";
            else
                btnUartConnect.Content = "Connect";

        }

        /// <summary>
        /// 启动UI变化
        /// </summary>
        private void RunBurn_UIChange(bool bRun)
        {
            if (btnStartBurn.Dispatcher.Thread != Thread.CurrentThread)
            {
                btnStartBurn.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (bRun)
                        btnStartBurn.Content = "停止烧录";
                    else
                        btnStartBurn.Content = "开始烧录";
                }));
            }
            else
            {
                if (bRun)
                    btnStartBurn.Content = "停止烧录";
                else
                    btnStartBurn.Content = "开始烧录";
            }

        }
        #endregion

        #region 连接断开

        /// <summary>
        /// 连接CAN
        /// </summary>
        bool Connect()
        {
            if (m_BOpen)
                return true;

            if (!m_UartOperate.OpenSerial(cbUartPort.Text))
                return false;

            m_BOpen = true;
            Connect_UIChange(true);
            return true;

        }

        /// <summary>
        /// 断开CAN
        /// </summary>
        bool DisConnect()
        {
            if (!m_BOpen)
                return true;

            if (!m_UartOperate.CloseSerial())
                return false;

            m_BOpen = false;
            Connect_UIChange(false);
            return true;
        }

        /// <summary>
        /// 连接串口
        /// </summary>
        private void btnConnectUart_Click(object sender, RoutedEventArgs e)
        {
            if (m_BOpen)
            {
                if (!DisConnect())
                {
                    Controls.Windows.UserMessageBox userMsg = new Windows.UserMessageBox(m_WinLocation, m_WinSize, "串口异常", "关闭失败!");
                    userMsg.ShowDialog();
                }
            }
            else
            {
                if (!Connect())
                {
                    Controls.Windows.UserMessageBox userMsg = new Windows.UserMessageBox(m_WinLocation, m_WinSize, "串口异常", "打开失败!");
                    userMsg.ShowDialog();
                }
            }


        }

        #endregion

        #region 数据收发



        #region 发送数据
        /// <summary>
        /// 等待回馈
        /// </summary>
        AutoResetEvent m_WaitReceive = new AutoResetEvent(false);
        /// <summary>
        /// 开始烧录
        /// </summary>
        private void btnStartBurn_Click(object sender, RoutedEventArgs e)
        {
            BurnHex();
        }

        /// <summary>
        /// 开始烧录、停止烧录
        /// </summary>
        private void BurnHex()
        {
            if (m_IsBurnning)
            {
                m_AutoSendTimer.Stop();
                m_IsBurnning = false;
            }
            else
            {

                
                m_InBootloader = m_McuFlashIsErased = false;

                //检查是否处于Bootloader中
                TextBlockAddMsg("检查是否处于Bootloader中", System.Windows.Media.Brushes.Black);
                m_UartOperate.WriteSerialByte(m_UartDataParse.g_CheckBootloaderCmd);
                m_WaitReceive.WaitOne(1000);
                if (!m_InBootloader)
                {
                    TextBlockAddMsg("请先进入bootloader模式", System.Windows.Media.Brushes.Red, 1);
                    return;
                }
                TextBlockAddMsg("已处于Bootloader中!", System.Windows.Media.Brushes.Blue);

                TextBlockAddMsg("检查是否已擦除Flash", System.Windows.Media.Brushes.Black);
                m_UartOperate.WriteSerialByte(m_UartDataParse.g_EraseFlashCmd);
                m_WaitReceive.WaitOne(5000);
                if (!m_McuFlashIsErased)
                {
                    TextBlockAddMsg("擦除超时", System.Windows.Media.Brushes.Red, 1);
                    return;
                }
                TextBlockAddMsg("擦除完毕!", System.Windows.Media.Brushes.Blue);

                TextBlockAddMsg("开始发送数据，总指令数: "+m_AllAddrData.AddrDataCollection.Count.ToString() , System.Windows.Media.Brushes.Black);

                m_SendDataCount = 0;
                m_ReceiveDataCount = 0;
                m_StartSendDateTime = DateTime.Now;

                Thread a = new Thread(() =>
                {
                    SendData(); //发送数据

                });
                a.Start();
                //m_AutoSendTimer.Start();

                m_IsBurnning = true;
            }

            RunBurn_UIChange(m_IsBurnning);
        }

        /// <summary>
        /// 发送等待接收
        /// </summary>
        AutoResetEvent m_WaitForReceiveReponse = new AutoResetEvent(false);
        /// <summary>
        /// 发送数据
        /// </summary>
        private void SendData()
        {

            while (m_SendDataCount < m_AllAddrData.AddrDataCollection.Count)
            {
                m_SendHasReponse = false;
                byte[] data = m_UartDataParse.UartDataPackage(m_AllAddrData.AddrDataCollection[m_SendDataCount]);
                m_UartOperate.WriteSerialByte(data); //实际发送
                                                     //m_UartDataParse.UserFlash_DataParseAddrData(data, (byte)data.Length); //测试
                m_SendDataCount++;
                double percent = m_SendDataCount * 1.0 / m_AllAddrData.AddrDataCollection.Count * 100;
                ProcessUpdate(percent);
                m_WaitForReceiveReponse.WaitOne(10000); //超时10s未接收到
                if(!m_SendHasReponse) //发送中断
                {
                    TextBlockAddMsg("发送中断: ", System.Windows.Media.Brushes.Red);
                    return;
                }
            }

            //发送完毕，停止发送

            {
                TimeSpan ts = DateTime.Now.Subtract(m_StartSendDateTime);

                TextBlockAddMsg("发送数据结束，耗时: " + ts.TotalSeconds.ToString() + "s", System.Windows.Media.Brushes.Black);
                BurnHex();
                m_UartOperate.WriteSerialByte(m_UartDataParse.g_DataEndCmd); //数据结束命令

            }


            #region 分解行发送，已弃用
            /*
            //发送一组4bytes数据
            byte[] data = m_UartDataParse.UartDataPackage(m_AllLineSplitSendData[m_SendDataCount]);
            m_UartOperate.WriteSerialByte(data);
            //TextBlockAddMsg("发送数据: "+BitConverter.ToString(data).Replace('-',' '), System.Windows.Media.Brushes.Black);

            m_SendDataCount++;
            

            //发送完毕，停止发送
            if (m_SendDataCount >= m_AllLineSplitSendData.Count)
            //if(m_AllSendData[m_SendDataCount].lineNum>=1) //测试，仅发送一行数据
            {
                TextBlockAddMsg("发送数据结束", System.Windows.Media.Brushes.Black);
                BurnHex();
            }
            */
            #endregion
        }

        #endregion

        #region 接收数据

        /// <summary>
        /// 接收数据，回调函数
        /// </summary>
        private void ReceiveData(byte[] datas)
        {
            
            //判断回馈类型
            ClassBootloaderForUart.CmdType cmdType = m_UartDataParse.ReceiveDataParse(ref datas);
            switch (cmdType)
            {
                case ClassBootloaderForUart.CmdType.EntryBootloader:
                    m_InBootloader = true;
                    BootloaderIndicator(true);
                    TextBlockAddMsg("成功进入Bootloader，4s内无操作进入用户程序!", System.Windows.Media.Brushes.Blue);
                    break;
                
                case ClassBootloaderForUart.CmdType.Reset:
                    m_InBootloader = false;
                    BootloaderIndicator(false);
                    TextBlockAddMsg("进入用户程序!", System.Windows.Media.Brushes.Blue);
                    break;
                case ClassBootloaderForUart.CmdType.Data:
                    m_ReceiveDataCount++;
                    m_SendHasReponse = true;
                    m_WaitForReceiveReponse.Set();
                    TextBlockChangeMsg("刷写进度","已发送指令: "+m_ReceiveDataCount.ToString()+" " , System.Windows.Media.Brushes.Gray);
                    break;
                case ClassBootloaderForUart.CmdType.DataEnd:
                    m_SendHasReponse = true;
                    //m_AllAddrData.AddrDataCollection[m_ReceiveDataCount]==
                    //double percent = m_ReceiveDataCount * 1.0 / m_AllAddrData.AddrDataCollection.Count * 100;
                    //TextBlockAddMsg("Receive " + m_ReceiveDataCount.ToString("00000") + " " + percent.ToString("0.00") + " : " + BitConverter.ToString(datas).Replace('-', ' '), System.Windows.Media.Brushes.Green);
                    TextBlockAddMsg("用户程序刷新完毕!", System.Windows.Media.Brushes.Blue);
                    #region 行拆解，已弃用
                    /*
                    string sendData = BitConverter.ToString(m_AllLineDatas[m_ReceiveDataCount]).Replace('-', ' ');
                    string recData = BitConverter.ToString(datas, 3, datas.Length - 5).Replace('-', ' ');
                    
                    bool result = sendData == recData;
                    TextBlockAddMsg("Receive " + m_ReceiveDataCount.ToString() + " "+ percent.ToString("0.00") +" : " + recData, System.Windows.Media.Brushes.Green);
                    if (!result) TextBlockAddMsg(result.ToString(),System.Windows.Media.Brushes.Red); //比对不正确
                    */
                    #endregion

                    
                    break;
                case ClassBootloaderForUart.CmdType.CheckBootloader:
                    
                    m_InBootloader = true;
                    m_WaitReceive.Set();
                    break;
                case ClassBootloaderForUart.CmdType.Earse:
                    
                    m_McuFlashIsErased = true;
                    m_WaitReceive.Set();
                    break;
                case ClassBootloaderForUart.CmdType.Other:
                    TextBlockAddMsg("Receive Other: " + BitConverter.ToString(datas), System.Windows.Media.Brushes.DarkOrange);
                    break;
                default: break;
            }
        }

        #endregion

        #endregion


        #region 事件

        protected override void OnVisible()
        {

        }

        /// <summary>
        /// 关闭前操作
        /// </summary>
        protected override void OnClosing()
        {

            if (m_BOpen)
            {
                DisConnect();
            }

            m_AutoSendTimer.Stop();

            //保存当前配置
            SaveConfig();
        }

        //下拉事件
        private void cbUartPort_DropDownOpened(object sender, EventArgs e)
        {
            GetUart();
        }

        /// <summary>
        /// 复位MCU
        /// </summary>
        private void btnResetMcu_Click(object sender, RoutedEventArgs e)
        {
            TextBlockAddMsg("Reset Command!", System.Windows.Media.Brushes.Black);
            m_UartOperate.WriteSerialByte(m_UartDataParse.g_ResetCmd);
        }

        /// <summary>
        /// 进入BootLoader
        /// </summary>
        private void btnEnterBootloader_Click(object sender, RoutedEventArgs e)
        {
            TextBlockAddMsg("Enter Bootloader Command!", System.Windows.Media.Brushes.Black);
            m_UartOperate.WriteSerialByte(m_UartDataParse.g_EntryBootloaderCmd);
        }

        /// <summary>
        /// 获取文件路径之后的操作
        /// </summary>
        /// <param name="filePath"></param>
        private void FileSelect(string filePath)
        {
            //读取数据
            string[] allLines = File.ReadAllLines(filePath);

            tbSelectedHexFilePath.Text = filePath;
            tbSelectedHexFilePath.ToolTip = "总行数: " + allLines.Length.ToString();

            TextBlockAddMsg("Hex文件加载完毕，" + tbSelectedHexFilePath.ToolTip, System.Windows.Media.Brushes.Green);

            m_AllAddrData = m_MicrochipCommonFunction.ConvertStringToDataList(allLines);

            

            #region 将一行数组拆分，方便传送，已弃用
            /*
            m_AllLineDatas = m_MicrochipCommonFunction.ConvertStringToHexBytes(ref allLines);

            //行数
            for (int i = 0; i < m_AllLineDatas.Count; i++)
            {
                //将一行数据分成4bytes一组
                for (int j = 0; j < m_AllLineDatas[i].Length; j += 4)
                {
                    // 命令类型1byte + 行号2byte2 + 数据长度1byte + 数据4bytes
                    ClassBootloaderForUart.OneFrame oneFrame = new ClassBootloaderForUart.OneFrame();
                    oneFrame.cmdType = ClassBootloaderForUart.CmdType.Data;
                    oneFrame.lineNum = i;
                    oneFrame.dataLength = Math.Min(m_AllLineDatas[i].Length - j, 4);  //剩下的不足4，则取小值
                    oneFrame.data = new byte[4];

                    for (int k = 0; k < oneFrame.dataLength; k++)
                    {
                        oneFrame.data[k] = m_AllLineDatas[i][j + k];
                    }

                    //一行最后的数据，数据长度加上0xA0，例如最后一个数据长度为2，则0xA2表示一行数据结束
                    if (j >= (m_AllLineDatas[i].Length - 4))
                    {
                        oneFrame.dataLength += 0xA0;
                    }

                    m_AllLineSplitSendData.Add(oneFrame);
                }
            }
            */
            #endregion


        }
        /// <summary>
        /// 选择Hex文件并解析
        /// </summary>
        private void btnSelectHexFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opfDialog = new OpenFileDialog();
            opfDialog.InitialDirectory = Directory.GetCurrentDirectory().ToString();
            opfDialog.Filter = "(*.hex)|*.hex|All files(*.*)|*.*";
            if (opfDialog.ShowDialog().Value)
            {
                FileSelect(opfDialog.FileName);
            }
        }

        #region 拖拽获取文件
        //获取文件名，添加进入
        private void TextBox_Drop(object sender, DragEventArgs e)
        {
            string filePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            FileSelect(filePath);
        }
        //图标效果
        private void TextBox_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effects = DragDropEffects.Link;
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }


        #endregion


        //清除信息
        private void btnClearMsg_Click(object sender, RoutedEventArgs e)
        {
            paraMsg.Inlines.Clear();
        }
        #endregion


        #region 配置文件
        /// <summary>
        /// 加载上一次配置
        /// </summary>
        private void LoadConfig()
        {
            ClassUser clsUser = new ClassUser();
            //CAN信息
            clsUser.GetNormalDefaultCanType();
            clsUser.GetNormalDefaultDeviceIndex();
            clsUser.GetNormalDefaultCanIndex();
            clsUser.GetNormalDefaultCanBaudRateIndex();
            clsUser.GetNormalDefaultMachineIndex();

            //发送
            clsUser.GetNormalDefaultSendTimers();
            clsUser.GetNormalDefaultSendGap();

            //自动保存的路径
            clsUser.GetNormalDefaultShowDataSaveInfo();

            //can筛选信息
            clsUser.GetNormalDefaultCanFilterActive();
            clsUser.GetNormalDefaultCanFilter();
            clsUser.GetNormalDefaultShieldContentId();

            //扭矩转速
            clsUser.GetNormalDefaultSendSpeed();
            clsUser.GetNormalDefaultSendTorque();
            clsUser.GetNormalDefaultSpeedUpDownLimit();


        }

        /// <summary>
        /// 保存本次配置
        /// </summary>
        private void SaveConfig()
        {


        }


        #endregion


        #region 其它函数

        /// <summary>
        /// 进度更新
        /// </summary>
        /// <param name="percent"></param>
        private void ProcessUpdate(double percent)
        {
            //判断是否跨线程更新UI
            if (psbProgress.Dispatcher.Thread != Thread.CurrentThread)
            {
                psbProgress.Dispatcher.BeginInvoke(new Action(() =>
                {
                    psbProgress.Value = percent;
                }));
            }
            else
                psbProgress.Value = percent;
        }

        /// <summary>
        /// 是否处于Bootloader中
        /// </summary>
        /// <param name="isOn"></param>
        private void BootloaderIndicator(bool isOn)
        {
            //判断是否跨线程更新UI
            if (sIDstatusOfMcuReset.Dispatcher.Thread != Thread.CurrentThread)
            {
                sIDstatusOfMcuReset.Dispatcher.BeginInvoke(new Action(() =>
                {
                    BootloaderResetIndicator(isOn);
                }));
            }
            else
                BootloaderResetIndicator(isOn);
        }
        /// <summary>
        /// 是否处于Bootloader中
        /// </summary>
        /// <param name="isOn"></param>
        private void BootloaderResetIndicator(bool isOn)
        {
            if (isOn)
            {
                sIDstatusOfMcuReset.IndicatorColor = BeatfanControls.Indicators.StateIndicator1.StateIndicatorColor.Red;
                sIDstatusOfBootloader.IndicatorColor = BeatfanControls.Indicators.StateIndicator1.StateIndicatorColor.Green;
            }
            else
            {
                sIDstatusOfMcuReset.IndicatorColor = BeatfanControls.Indicators.StateIndicator1.StateIndicatorColor.Green;
                sIDstatusOfBootloader.IndicatorColor = BeatfanControls.Indicators.StateIndicator1.StateIndicatorColor.Red;
            }
        }



        private void TextBlockAddMsg(string msg, System.Windows.Media.Brush color, int enterNumBefore = 0, int enterNumAfter = 1)
        {
            //判断是否跨线程更新UI
            if (paraMsg.Dispatcher.Thread != Thread.CurrentThread)
            {
                paraMsg.Dispatcher.BeginInvoke(new Action(() =>
                {
                    ParagraphAddMsg(msg,color,enterNumBefore, enterNumAfter);
                }));
            }
            else
                ParagraphAddMsg(msg, color, enterNumBefore, enterNumAfter);
        }

        /// <summary>
        /// 信息显示
        /// enterNumBefore为信息前方回车数据
        /// </summary>
        private void ParagraphAddMsg(string msg, System.Windows.Media.Brush color, int enterNumBefore = 0, int enterNumAfter = 1)
        {
            System.Windows.Documents.Run line = new System.Windows.Documents.Run();
            string data = "";
            for (int i = 0; i < enterNumBefore; i++)
            {
                data += "\r\n";
            }
            data += msg;
            for (int i = 0; i < enterNumAfter; i++)
            {
                data += "\r\n";
            }
            line.Text = data;
            line.Foreground = color;
            paraMsg.Inlines.Add(line);
            rtbMsg.ScrollToEnd();
        }

        private void TextBlockChangeMsg(string id,string msg, System.Windows.Media.Brush color, int enterNumBefore = 0, int enterNumAfter = 1)
        {
            //判断是否跨线程更新UI
            if (paraMsg.Dispatcher.Thread != Thread.CurrentThread)
            {
                paraMsg.Dispatcher.BeginInvoke(new Action(() =>
                {
                    ParagraphChangeMsg(id,msg, color, enterNumBefore, enterNumAfter);
                }));
            }
            else
                ParagraphChangeMsg(id,msg, color, enterNumBefore, enterNumAfter);
        }

        /// <summary>
        /// 修改信息显示，若不存在，则创建一个
        /// enterNumBefore为信息前方回车数据
        /// </summary>
        private void ParagraphChangeMsg(string id,string msg, System.Windows.Media.Brush color, int enterNumBefore = 0, int enterNumAfter = 1)
        {

            if (paraMsg.Inlines.LastInline.Name == id)
            {
                string data = "";
                for (int i = 0; i < enterNumBefore; i++)
                {
                    data += "\r\n";
                }
                data += msg;
                for (int i = 0; i < enterNumAfter; i++)
                {
                    data += "\r\n";
                }
                System.Windows.Documents.Run line = paraMsg.Inlines.LastInline as System.Windows.Documents.Run;
                line.Text = data;
                line.Foreground = color;
            }
            else
            {
                System.Windows.Documents.Run line = new System.Windows.Documents.Run();
                line.Name = id;


                string data = "";
                for (int i = 0; i < enterNumBefore; i++)
                {
                    data += "\r\n";
                }
                data += msg;
                for (int i = 0; i < enterNumAfter; i++)
                {
                    data += "\r\n";
                }
                line.Text = data;
                line.Foreground = color;
                paraMsg.Inlines.Add(line);
                rtbMsg.ScrollToEnd();
            }
        }


        #endregion

        private void btnShowMsg_Click(object sender, RoutedEventArgs e)
        {
            DataGridTest dgt = new DataGridTest(m_WinLocation, m_WinSize, ref m_UartDataParse.test_AllAddrData);
            dgt.ShowDialog();
        }


    }
}
